package cn.hrlly.tao2shou.callback;

import com.google.gson.Gson;
import com.zhy.http.okhttp.callback.Callback;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.IOException;

import cn.hrlly.tao2shou.R;
import cn.hrlly.tao2shou.domain.User;
import cn.hrlly.tao2shou.listener.OnLoginListener;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 登录联网后的返回数值操作
 * User: gengliang
 * Date: 2016-01-22
 * Time: 08:05
 */

public class LoginCallback extends Callback<User>  {

    private OnLoginListener onLoginListener;

    public LoginCallback(OnLoginListener onLoginListener){
        this.onLoginListener=onLoginListener;
    }

    @Override
    public User parseNetworkResponse(Response response) throws IOException {
        String string=response.body().string();
        User user=new Gson().fromJson(string,User.class);
        System.out.println(string);
        return user;
    }

    @Override
    public void onError(Request request, Exception e) {
        onLoginListener.loginFail(R.string.network_connection_error);
    }

    @Override
    public void onResponse(User user) {
        if(user.getStatus()==0)
            onLoginListener.loginSuccess(user);
        else
            onLoginListener.loginFail(user.getErrorMessage());
    }
}
